//===============基础===============

//添加net窗口
Scene_Base.prototype.addNetWindow = function (window) {
    this._netWindowLayer.addChild(window);
}

//创建net窗口层
Scene_Map.prototype.createNetWindowLayer = function () {
    this._netWindowLayer = new WindowLayer();
    this._netWindowLayer.x = (Graphics.width - Graphics.boxWidth) / 2;
    this._netWindowLayer.y = (Graphics.height - Graphics.boxHeight) / 2;
    this.addChild(this._netWindowLayer);
}

//
sun_rmnet.isAnyButtonPressed = Scene_Map.prototype.isAnyButtonPressed;
Scene_Map.prototype.isAnyButtonPressed = function () {
    for (const button of this._netButtons) {
        if (button.isPressed()) return;
    }
    return sun_rmnet.isAnyButtonPressed.call(this);
}

//按钮精灵
class Sprite_NetButton extends Sprite_Clickable {
    initialize(x, y, bitmap, fun) {
        super.initialize();
        this._clickHandler = null;
        this._coldFrame = null;
        this._hotFrame = null;
        this._state = null;
        this.setButtonBitmap(bitmap);
        this.setupFrames();
        this.move(x, y);
        this.setClickHandler(fun);
    }

    setupFrames() {
        const w = this.width;
        const h = this.height / 2;
        this.setColdFrame(0, 0, w, h);
        this.setHotFrame(0, h, w, h);
        this.updateFrame();
        this.updateOpacity();
    }

    setButtonBitmap(bitmap) {
        if (typeof (bitmap) === "string") {
            this.bitmap = new Bitmap(200, 120);
            this.bitmap.fillRoundRect(0, 0, 200, 60, ColorManager.textColor(9));
            this.bitmap.fillRoundRect(0, 60, 200, 60, ColorManager.textColor(8));
            this.bitmap.fontSize = 45;
            this.bitmap.drawText(bitmap, 0, 0, 200, 60, "center");
            this.bitmap.drawText(bitmap, 0, 60, 200, 60, "center");
        } else {
            this.bitmap = bitmap;
        }
    }

    update() {
        Sprite_Clickable.prototype.update.call(this);
        this.updateFrame();
        this.updateOpacity();
        this.processTouch();
    }

    updateFrame() {
        const frame = this.isPressed() || this._state === "select" ? this._hotFrame : this._coldFrame;
        if (frame) {
            this.setFrame(frame.x, frame.y, frame.width, frame.height);
        }
    }

    updateOpacity() {
        this.opacity = this._pressed ? 255 : 192;
    }

    setClickHandler(method) {
        this._clickHandler = method;
    }

    setColdFrame(x, y, width, height) {
        this._coldFrame = new Rectangle(x, y, width, height);
    }

    setHotFrame(x, y, width, height) {
        this._hotFrame = new Rectangle(x, y, width, height);
    }

    onClick() {
        if (this._clickHandler) {
            this._clickHandler();
        } else {
            Input.virtualClick(this._buttonType);
        }
    }
}




//新增提示窗口
class Window_Tip extends Window_MapName {
    initialize(rect) {
        super.initialize(rect);
        this._text = "";
    }

    pop(text) {
        this._text = text;
        this._display = true;
        this.refresh();
        this._showCount = 150;
    }

    refresh() {
        const text = this._text;
        this.contents.clear();
        const width = this.innerWidth;
        this.drawBackground(0, 0, width, this.lineHeight());
        this.drawText(text, 0, 0, width, "center");
    }

    open() {
        this.refresh();
        if ($gameMap.displayName()) {
            this.pop($gameMap.displayName());
        }
    }
}

//脸图
Window_Base.prototype.drawFace = function (
    faceName, faceIndex, x, y, width, height
) {
    const bitmap = ImageManager.loadFace(faceName);
    bitmap.addLoadListener(() => {
        const pw = ImageManager.faceWidth;
        const ph = ImageManager.faceHeight;
        const sw = Math.min(width, pw);
        const sh = Math.min(height, ph);
        const dx = Math.floor(x + Math.max(width - pw, 0) / 2);
        const dy = Math.floor(y + Math.max(height - ph, 0) / 2);
        const sx = (faceIndex % 4) * pw + (pw - sw) / 2;
        const sy = Math.floor(faceIndex / 4) * ph + (ph - sh) / 2;
        this.contents.blt(bitmap, sx, sy, sw, sh, dx, dy);
    })
}

//判断该点是否在窗口范围内
Window_Base.prototype.contains = function (x, y) {
    if (this.width <= 0 || this.height <= 0) {
        return false;
    }
    if (x >= this.x && x < this.x + this.width) {
        if (y >= this.y && y < this.y + this.height) {
            return true;
        }
    }
    return false;
}

//Net文字绘制
Window_Base.prototype.drawNetText = function (textGroup, x, y, maxWidth) {
    let text = "";
    if (typeof textGroup === "string") {
        text = [textGroup];
    } else if (textGroup.constructor === Array) {
        if (textGroup.length === 0) return;
        text = textGroup;
        // for (const c of textGroup) {
        //     text += c;
        // }
    }
    const textState = this.createNetTextState(text, x, y, maxWidth);
    this.processNetText(textState);
}

Window_Base.prototype.createNetTextState = function (textArray, x, y, maxWidth) {
    const textState = {};
    textState.textArray = textArray;
    textState.text = "";//this.convertEscapeCharacters(text);
    textState.x = x;
    textState.y = y;
    textState.index = 0;
    textState.width = 0;
    textState.maxWidth = maxWidth;
    textState.height = this.contents.fontSize;
    textState.startX = textState.x;
    textState.startY = textState.y;
    textState.buffer = "";
    return textState;
}

Window_Base.prototype.processNetText = function (textState) {
    for (let i = 0; i < textState.textArray.length; i++) {
        textState.text = this.convertEscapeCharacters(textState.textArray[i]);
        while (textState.index < textState.text.length) {
            this.processNetCharacter(textState);
        }
        this.processNetNewParagraph(textState);
    }
    // this.flushNetTextState(textState);
}

Window_Base.prototype.processNetCharacter = function (textState) {
    const c = textState.text[textState.index++];
    if (c.charCodeAt(0) < 0x20) {
        // this.flushNetTextState(textState);
        this.processNetControlCharacter(textState, c);
    } else {
        textState.buffer = c;
        this.flushNetTextState(textState);
    }
}

Window_Base.prototype.flushNetTextState = function (textState) {
    const text = textState.buffer;
    const width = this.textWidth(text);
    const height = textState.height;
    if ((textState.width + width) > textState.maxWidth) {
        this.processNetNewLine(textState);
    }
    const x = textState.x;
    const y = textState.y;
    this.contents.drawText(text, x, y, width, height);
    textState.buffer = "";
    textState.x += width;
    textState.width += width;
}

Window_Base.prototype.processNetControlCharacter = function (textState, c) {
    if (c === "\n") {
        this.processNetNewLine(textState);
    }
    if (c === "\x1b") {
        const code = this.obtainEscapeCode(textState);
        this.processEscapeCharacter(code, textState);
    }

    // if (textState.textArray[textState.index] === "n") {
    //     this.processNetNewLine(textState);
    // } else {
    //     this.flushNetTextState(textState);
    // }
}

Window_Base.prototype.processNetNewLine = function (textState) {
    textState.x = textState.startX;
    textState.y += textState.height + 5;
    textState.width = 0;
    this._overallHeight += textState.height + 5;
}

Window_Base.prototype.processNetNewParagraph = function (textState) {
    textState.x = textState.startX;
    textState.y += textState.height + 20;
    textState.width = 0;
    textState.index = 0;
    this._overallHeight += textState.height + 20;
}

//新增Net背景
Window_Base.prototype.setBackgroundType = function (type) {
    if (type === 0) {
        this.opacity = 255;
    } else {
        this.opacity = 0;
    }
    if (type === 1) {
        this.showBackgroundDimmer();
    } else {
        this.hideBackgroundDimmer();
    }
    if (type === 3) {
        this.showNetBackground();
    } else {
        this.hideNetBackground();
    }
}

Window_Base.prototype.showNetBackground = function (type) {
    if (!this._netBackSprite) {
        this.createNetBackSprite();
    }
    const bitmap = this._netBackSprite.bitmap;
    if (bitmap.width !== this.width || bitmap.height !== this.height) {
        this.refreshNetBackBitmap();
    }
    this._netBackSprite.visible = true;
    this.updateNetBackground();
}

Window_Base.prototype.createNetBackSprite = function () {
    this._netBackSprite = new Sprite();
    this._netBackSprite.bitmap = new Bitmap(0, 0);
    this._netBackSprite.x = 0;
    this.addChildToBack(this._netBackSprite);
}

Window_Base.prototype.hideNetBackground = function () {
    if (this._netBackSprite) {
        this._netBackSprite.visible = false;
    }
}

Window_Base.prototype.updateNetBackground = function () {
    if (this._netBackSprite) {
        this._netBackSprite.opacity = this.openness;
    }
}

Window_Base.prototype.refreshNetBackBitmap = function () {

}

sun_rmnet.createCharacters = Spriteset_Map.prototype.createCharacters;
Spriteset_Map.prototype.createCharacters = function () {
    sun_rmnet.createCharacters.call(this);
    // this._netCharacterSprites = [];
    const netCharacterSprites = [];
    const players = NetManager.players;
    for (others of players.values()) {
        others.setLocation();
        others.createSprite();
        netCharacterSprites.push(others.sprite());
    }
    for (const sprite of netCharacterSprites) {
        this._tilemap.addChild(sprite);
    }
}

//人物精灵初始化时添加名字精灵
sun_rmnet.spriteCharacterInitialize = Sprite_Character.prototype.initialize;
Sprite_Character.prototype.initialize = function (character) {
    sun_rmnet.spriteCharacterInitialize.call(this, character);
    this.createNameSprite();
}

//创建头顶显示精灵
Sprite_Character.prototype.createNameSprite = function () {
    const character = this._character
    const className = character.constructor.name;
    let name, color;
    switch (className) {
        case "Game_Event":
            name = character.event().note;
            color = "#FFA500";
            break;
        case "Game_Player":
            name = NetManager.actor.name();
            color = "#F5F5F5";
            break;
        case "Net_Character":
            name = character.name();
            color = "#4169E1";
            break;
        default:
            return;
    }
    if (name === "") return;
    const sprite = new Sprite_PlayerName(character);
    sprite._character = character;
    character._nameSprite = sprite;
    this.addChild(sprite);
}

//===============玩家的名字称号精灵类===============
class Sprite_PlayerName extends Sprite {
    initialize(character) {
        super.initialize();
        this._character = character;
        this.createBitmap();//绘制名字
        this.refresh();	//刷新信息
    }

    createBitmap() {
        this.bitmap = new Bitmap(144, 60);
        this.anchor.x = 0.5;
        this.anchor.y = 1;
        this.x = 0;
        this.y = -45;
        this.bitmap.fontSize = 20;
    }

    refresh() {
        this.bitmap.clear();
        this.drawState();
        this.drawName();
    }

    drawState() {
        const character = this._character;
        if (character.constructor.name === "Net_Character") {
            const mapState = character.getMapState();
            this.bitmap.textColor = "#808080";
            switch (mapState) {
                case "map":
                    if (this._titleId === 0) return;
                    break;
                case "battle":
                    this.bitmap.drawText("战斗中...", 0, 5, 144, 20, 'center');
                    break;
                case "menu":
                    this.bitmap.drawText("冥想中...", 0, 5, 144, 20, 'center');
                    break;
                default:
                    break;
            }
        }
    }

    drawName() {
        this.setup();
        const name = this._name;
        this.bitmap.textColor = this._nameColor;
        this.bitmap.drawText(name, 0, 35, 144, 20, 'center');
    }

    setup() {
        const character = this._character
        const className = character.constructor.name;
        let name, color;
        switch (className) {
            case "Game_Event":
                name = character.event().note;
                color = "#FFA500";
                break;
            case "Game_Player":
                name = NetManager.actor.name();
                color = "#F5F5F5";
                break;
            case "Net_Character":
                name = character.name();
                color = "#4169E1";
                break;
            default:
                break;
        }
        this._name = name;
        this._nameColor = color;
    }
}

//===============地图===============

//屏蔽掉主菜单按键
Scene_Map.prototype.isMenuEnabled = function () {
    return false;
}

//将地图名字窗口改为tip窗口
Scene_Map.prototype.createMapNameWindow = function () {
    const ww = Graphics.boxWidth;
    const wh = 150;
    const wx = 0;
    const wy = 50;
    const rect = new Rectangle(wx, wy, ww, wh);
    this._tipWindow = new Window_Tip(rect);
    this._mapNameWindow = this._tipWindow;
    this.addWindow(this._tipWindow);
}

//在Scene_Map中创建新的窗口
sun_rmnet.sceneMapCreateDisplayObjects = Scene_Map.prototype.createDisplayObjects;
Scene_Map.prototype.createDisplayObjects = function () {
    sun_rmnet.sceneMapCreateDisplayObjects.call(this);
    this._netButtons = [];
    this.createNetWindowLayer();
    this.createAllNetWindow();
}

//创建所有net窗口
Scene_Map.prototype.createAllNetWindow = function () {
    this.createMyInfoWindows();
    // this.createTargetWindows();
    this.createPlayerCommandWindows();
    this.createConfirmWindows();
    this.createPartyMemberWindows();
}

//玩家信息窗口：该窗口为地图场景中显示于右上角的你自己的信息的窗口
//包括头像、名字、登记、职业、血条、蓝条
Scene_Map.prototype.createMyInfoWindows = function () {
    const rect = this.myInfoWindowsRact();
    this._myInfoWindow = new Window_MyInfo(rect);
    this.addNetWindow(this._myInfoWindow);
}

Scene_Map.prototype.myInfoWindowsRact = function () {
    const ww = 260;
    const wh = 120;
    const wx = Graphics.boxWidth - ww;
    const wy = 0;
    return new Rectangle(wx, wy, ww, wh);
}

//玩家选项窗口：该窗口为点击目标信息窗口后唤出的与其他玩家交互的窗口
//目前只有组队功能
Scene_Map.prototype.createPlayerCommandWindows = function () {
    this._playerCommandWindow = new Sprite_PlayerCommand();
    // this._playerCommandWindow.setHandler("party", NetMap.commandParty);
    // this._playerCommandWindow.hide();
    // this._playerCommandWindow.deactivate();
    this.addNetWindow(this._playerCommandWindow);
}

// Scene_Map.prototype.playerCommandWindowsRact = function () {
//     const ww = 200//this.mainCommandWidth();
//     const wh = 200//this.calcWindowHeight(1, true);
//     const wx = 0//(Graphics.boxWidth - ww) / 2;
//     const wy = 0//170;
//     return new Rectangle(wx, wy, ww, wh);
// }

//交互确认窗口：该窗口为处理收到其他玩家交互请求的确认窗口
//用于给玩家同意或拒绝该请求
Scene_Map.prototype.createConfirmWindows = function () {
    const rect = this.confirmWindowsRact();
    this._confirmWindow = new Window_PlayerConfirm(rect);
    this._confirmWindow.hide();
    this.addNetWindow(this._confirmWindow);
}

Scene_Map.prototype.confirmWindowsRact = function () {
    const ww = 500;
    const wh = 200;
    const wx = (Graphics.boxWidth - ww) / 2;
    const wy = (Graphics.boxHeight - wh) / 2;
    return new Rectangle(wx, wy, ww, wh);
}

//队伍成员窗口：该窗口为显示队伍中成员列表的窗口
//还未决定点击该窗口直接进入交互场景还是直接唤出对每个玩家操作的选项窗口
Scene_Map.prototype.createPartyMemberWindows = function () {
    const rect = this.partyWindowsRact();
    this._partyWindow = new Window_PartyMember(rect);
    this.addWindow(this._partyWindow);
}

Scene_Map.prototype.partyWindowsRact = function () {
    const ww = 220;
    const wh = 300;
    const wx = 0;
    const wy = (Graphics.boxHeight - wh) / 2;
    return new Rectangle(wx, wy, ww, wh);
}

Scene_Map.prototype.openPlayerCommand = function (x, y) {
    this._playerCommandWindow.open(x, y);
}

sun_rmnet.isMapTouchOk = Scene_Map.prototype.isMapTouchOk;
Scene_Map.prototype.isMapTouchOk = function () {
    return sun_rmnet.isMapTouchOk.call(this) && !this._playerCommandWindow.visible;
}

/* 
 *1.触摸地图时判断该点是否有其他玩家如果有触发交互
 *2.如果触摸的在某些窗口上，不触发移动（玩家列表、目标窗口）
 */
Scene_Map.prototype.onMapTouch = function () {
    const x = $gameMap.canvasToMapX(TouchInput.x);
    const y = $gameMap.canvasToMapY(TouchInput.y);
    if (NetManager.players.size > 0 && this.findPlayers(x, y)) {
        return;
    }
    $gameTemp.setDestination(x, y);
}

//地图点击后，寻找点击位置的玩家
Scene_Map.prototype.findPlayers = function (x, y) {
    const players = NetManager.players;
    const list = [];
    for (const player of players.entries()) {
        if (player[1].x === x && player[1].y === y) {
            list.push(player[0]);
        }
    }
    if (list.length > 0) {
        if (list.length === 1) {
            const uid = list[0];
            const cx = $gameMap.mapToCanvasX(x);
            const cy = $gameMap.mapToCanvasY(x);
            this._playerCommandWindow.setPlayer(uid);
            this.openPlayerCommand(cx, cy);
            return true;
        }
        const sprite = players.get(list[0])._sprite;
        const sx = sprite.x;
        const sy = sprite.y;
        this.openPlayerListWindow(list, sx, sy);
        return true;
    }
    return false;
}

/**
 * 窗口-玩家信息
 * 在地图场景右上角显示玩家基础状态的窗口
 * @class
 */
class Window_MyInfo extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this.setActor();
        this.setBackgroundType(3);
        this.createsSprite();
        this.refresh();
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#F9F9F9";//"#2D2D2D";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h, c, 10);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }

    setActor() {
        this._actor = NetManager.actor;
    }

    refresh() {
        this.contents.clear();
        this.drawName();
        this.drawFace();
    }

    createsSprite() {
        // const x = 15;
        // const y = 38;
        // this.createGaugeSprite(x, y);
        this.createFaceSprite(this.width - this.height + 5, 5);
    }

    createFaceSprite(x, y) {
        const w = this.height - 10;
        const h = this.height - 10;
        const c = "#ADADAD";
        this._faceBitmap = new Bitmap(w, h * 2);
        this._faceBitmap.fillRoundRect(0, 0, w, h, c, 5);
        this._faceSprite = new Sprite_NetButton(x, y, this._faceBitmap, this.onClick.bind(this));
        this.addChild(this._faceSprite);
    }

    drawName() {
        const x = 6;
        const y = -8;
        const w = 120;
        const name = this._actor.name();//#60C1B5
        const lv = this._actor.level;
        const className = $dataClasses[this._actor._classId].name;
        const nameTextWidth = this.textWidth(name);
        const text = `${name}  lv${lv}`;
        this.contents.fontSize = 20;
        this.drawText(text, x, y, w, "left");
    }

    drawFace() {
        if (!this._actor) return;
        const w = this._faceBitmap.width - 10;
        const h = this._faceBitmap.height / 2 - 10;
        const x = 5;
        const y = 5;
        const faceName = this._actor._faceName;
        const faceIndex = this._actor._faceIndex;
        const bitmap = ImageManager.loadFace(faceName);
        bitmap.addLoadListener(() => {
            const pw = ImageManager.faceWidth;
            const ph = ImageManager.faceHeight;
            const sx = (faceIndex % 4) * pw;
            const sy = Math.floor(faceIndex / 4) * ph;
            this._faceBitmap.blt(bitmap, sx, sy, pw, ph, x, y, w, h);
        })
    }

    createGaugeSprite(x, y) {
        this._hpGauge = new Sprite_MapGauge();
        this._mpGauge = new Sprite_MapGauge();
        this._hpGauge.set(this._actor, "hp");
        this._mpGauge.set(this._actor, "mp");
        this._hpGauge.move(x, y);
        this._mpGauge.move(x, y + 20);
        this.addChild(this._hpGauge);
        this.addChild(this._mpGauge);
    }

    onClick() {
        SceneManager.push(Scene_Menu);
    }
}

//===============玩家选项窗口===============
class Sprite_PlayerCommand extends Sprite_Clickable {
    initialize() {
        super.initialize();
        this.hide();
        this.scale.set(0, 0);
        this.createBitmap();
        this.anchor.x = 0.5;
        this.anchor.y = 0.5;
        this._player = NaN;
        this._isOpening = false;
        this._isCloseing = false;
    }

    setPlayer(uid) {
        this._player = uid;
    }

    createBitmap() {
        const bitmap = new Bitmap(200, 200);
        //测试用，后期换直接成图片素材
        bitmap.drawCircle(100, 100, 100, "#F9F9F9");
        bitmap.textColor = "#2D2D2D";
        bitmap.fontSize = 24;
        bitmap.drawText("关闭", 0, 88, 200, 24, "center");
        bitmap.drawText("查看", 0, 6, 200, 24, "center");
        bitmap.drawText("好友", 20, 48, 160, 24, "left");
        bitmap.drawText("组队", 20, 48, 160, 24, "right");
        bitmap.drawText("交易", 20, 138, 160, 24, "left");
        bitmap.drawText("工会", 20, 138, 160, 24, "right");
        bitmap.drawText("预留", 0, 170, 200, 24, "center");
        this.bitmap = bitmap;
    }

    open(x, y) {
        if (this.visible) return;
        const tileWidth = $gameMap.tileWidth();
        const tileHeight = $gameMap.tileHeight();
        this.move(TouchInput.x, TouchInput.y);
        // this.move(x + tileWidth / 2, y - tileHeight / 2);
        this.show();
        this._isOpening = true;
    }

    close() {
        this._isCloseing = true;
    }

    update() {
        super.update();
        if (this._isOpening && !this._isCloseing) {
            const scale = this.scale.x + 0.05;
            this.scale.set(scale, scale);
            if (scale >= 1) this._isOpening = false;
            return;
        }

        if (this._isCloseing) {
            const scale = Math.max(0, this.scale.x - 0.05);
            this.scale.set(scale, scale);
            if (scale <= 0) {
                this._isCloseing = false
                this.hide();
            }
        }
    }

    processTouch() {
        if (this.isClickEnabled()) {
            if (this.isBeingTouched()) {
                if (!this._hovered && TouchInput.isHovered()) {
                    this._hovered = true;
                    this.onMouseEnter();
                }
                if (TouchInput.isTriggered()) {
                    this._pressed = true;
                    this.onPress();
                }
            } else {
                if (this._hovered) {
                    this.onMouseExit();
                }
                this._pressed = false;
                this._hovered = false;
            }
            if (this._pressed && TouchInput.isReleased()) {
                this._pressed = false;
                this.onClick(TouchInput.x, TouchInput.y);
            }
        } else {
            this._pressed = false;
            this._hovered = false;
        }
    }

    onClick(x, y) {
        const ox = this.x// + this.width / 2;
        const oy = this.y// + this.height / 2;
        const r = Math.sqrt(Math.pow(x - ox, 2) + Math.pow(y - oy, 2));
        const angle = this.getAngle(x - ox, y - oy);
        const index = Math.floor(angle / 60);
        if (r < this.width / 4) {
            this.close();

            return;
        }
        switch (index) {
            case 0:
                const uid = this._player;
                NetManager.invitingTeams(uid);
                break;
            default:
                break;
        }
        this.close();
    }

    getAngle(x, y) {
        const radian = Math.atan2(y, x);
        let angle = radian * 180 / Math.PI;
        //处理成以x轴正方向为基准，逆时针旋转的角度
        angle = angle < 0 ? -angle : 360 - angle;
        return angle;
    }
}

//===============交互确认窗口===============
class Window_PlayerConfirm extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this._text = "";
        this._mode = "";
        this._data = {};
        this.createButton();
        this.refresh();
    }

    createButton() {
        this.createAgreeButton();
        this.createRefuseButton();
    }

    refresh() {
        this.contents.clear();
        // this.contents.fontSize = 35;
        this.drawText(this._text, 0, 5, this.width - 20, "left");
    }

    setText(text) {
        this._text = text;
    }

    setMode(mode) {
        this._mode = mode;
    }

    setData(type, data) {
        this._data[type] = data;
    }

    createAgreeButton() {
        const text = "同意";
        const fun = this._agree.bind(this);
        const button = new Sprite_NetButton(30, 120, text, fun);
        this.addChild(button);
        this._logonButton = button;
    }

    createRefuseButton() {
        const text = "拒绝";
        const fun = this._refuse.bind(this);
        const button = new Sprite_NetButton(270, 120, text, fun);
        this.addChild(button);
        this._logonButton = button;
    }

    _agree() {
        const party = new Net_Party();
        const data = this._data[this._mode];
        party.setup(data);
        NetManager.party = party;
        this.hide();
        NetManager.partyAgree();
    }

    _refuse() {
        this._data[this._mode] = null;
        this.hide();
        NetManager.partyRefuse();
    }

    show() {
        this.refresh();
        super.show();
    }

    hide() {
        this._text = "";
        this._mode = "";
        super.hide();
    }
}

//===============队伍成员窗口===============
class Window_PartyMember extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this.setBackgroundType(3);
        this.createButton();
        this.refresh();
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#F9F9F9";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h, c, 10);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }

    createButton() {
        const x = 10;
        const y = this.height - 70;
        const text = "离开队伍";
        const button = new Sprite_NetButton(x, y, text, NetManager.leaveParty.bind(NetManager));
        this.addChild(button);
        this._leaveButton = button;
        SceneManager._scene._netButtons.push(button);
    }

    refresh() {
        this.contents.clear();
        this.drawPartyInfo();
    }

    drawPartyInfo() {
        const party = NetManager.party;
        if (!party) return;
        const leader = party.leader();
        const member = party.member();

        const x = 5;
        let y = 0;
        this.drawMember(leader, x, y);
        y += 35;
        for (const uid of member) {
            this.drawMember(uid, x, y);
            y += 35;
        }
    }

    drawMember(uid, x, y) {
        let data;
        if (uid === NetManager.actor.uid()) {
            const actor = NetManager.actor;
            data = { "name": actor.name(), "classId": actor._classId, "mapId": 0 };
        } else {
            data = NetManager.party.data().get(uid);
        }
        if (data) {
            const { name, classId, mapId } = data;
            const index = 320 + classId;
            const mapName = mapId === $gameMap.mapId() ? "附近" : $dataMapInfos[mapId]?.name;
            this.drawIcon(index, x, y);
            this.drawText(name, x + 35, y, this.width - 35, "left");
            this.drawText(mapName, x + 35, y, this.width, "right");
            return;
        }
        this.drawText("??????????", x, y, this.width, "left");
    }
}